package com.dhecp.project.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.project.system.domain.SysUserDefinedForField;
import com.dhecp.project.system.service.SysUserDefinedForFieldService;

/**
 * 用户自定义列设置 控制层
 * @author DH-Xxj
 * @version 2019年11月12日
 */

@Controller
@RequestMapping("/system/userdefinedfields")
public class SysUserDefinedForFieldController extends BaseController {
    private String prefix = "system/userdefinedfields";

    @Autowired
    private SysUserDefinedForFieldService userDefinedForFieldService;

    /**
     * 打开用户自定义列设置
     * @param moduleTable 必要参数：当前设置列的功能唯一标识
     * @return
     * @author DH-Xxj
     */
    @GetMapping()
    public ModelAndView userDefinedForFieldList(@RequestParam Map<String, Object> params){
        ModelAndView mv = this.getModelAndView();
        mv.setViewName(prefix + "/userdefinedfields");
    	return mv;
    }
    
    /**
     * 获取用户自定义列设置数据
     * @param moduleTable 必要参数：当前设置列的功能唯一标识
     * @return
     * @author DH-Xxj
     */
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo userDefinedForFieldSelectListJson(@RequestParam Map<String, Object> params){
		List<SysUserDefinedForField> list = new ArrayList<SysUserDefinedForField>();
    	if (params.get("moduleTable") != null && params.get("moduleTable") != ""){
    		try{
    			list = userDefinedForFieldService.queryUserDefinedForField(params);
    		}catch(Exception e){
    			return getDataTable(this.getExceptionMessage(e).toString());
    		}
    	}else{
    		return getDataTable("请传入当前设置列的功能唯一标识（moduleTable）。");
    	}
        return getDataTable(list);
    }
    
    /**
     * 保存用户自定义列设置
     * @param moduleTable 必要参数：当前设置列的功能唯一标识
     * @param infoItems 用户自定义设置实体类对应的json格式字符串(包括：moduleTable,fields,fieldName,fieldFrontHidden,fieldFrontFixed,fieldFrontOrder,fieldFrontWidth)
     * @return
     * @author DH-Xxj
     */
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult userDefinedForFieldEditPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
		if (params.get("moduleTable") == null || params.get("moduleTable") == ""){
			return AjaxResult.error("缺少必要参数：moduleTable【当前设置列的功能唯一标识】。");
		}
        if (params.get("infoItems") == null || params.get("infoItems") == ""){
        	return AjaxResult.error("缺少必要参数：infoItems【用户自定义设置实体类对应的json格式字符串】。");
        }
        try {
            ajaxResult= userDefinedForFieldService.updateUserDefinedForField(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 恢复系统设置（删除用户自定义设置列）
     * @param moduleTable 必要参数：当前设置列的功能唯一标识
     * @return
     * @author DH-Xxj
     */
    @PostMapping("/resetpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult userDefinedForFieldResetPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
		if (params.get("moduleTable") == null || params.get("moduleTable") == ""){
			return AjaxResult.error("缺少必要参数：moduleTable【当前设置列的功能唯一标识】。");
		}
        try {
            ajaxResult= userDefinedForFieldService.deleteUserDefinedForField(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    
}
